<?php

header("Content-Type: text/html;charset=utf8");

class AdminDb {

    private $mysql;

    public function __construct(InMysql $mysql = null) {
        if ($mysql == null) {
            $this->mysql = new InMysql();
        } else {
            $this->mysql = $mysql;
        }
    }

    public function login($username, $password) {
        $username = addslashes($username);
        $password = addslashes($password);
        $sql = "select * from admin where name = '" . $username . "' and pwd = '" . $password . "' and status = 1"; //status=1
        return $this->mysql->fetchData($sql);
    }

    public function listAdmin() {
        $sql = "select * from admin";
        return $this->mysql->fetchData($sql);
    }

    /**
     * 添加管理员
     * @param type $name
     * @param type $pwd
     * @param type $p
     * @param type $nick
     * @return type
     */
    public function addAdmin($name, $pwd,$addtime, $p, $nick) {
        $sql="insert into admin(name,pwd,ctime,p,nick)values('$name','$pwd','$addtime','$p','$nick')";
        return $this->mysql->insertData($sql);
    }

    /**
     * 查询管理员
     * @param type $name
     * @return type
     */
    public function queryAdmin($username){
        $sql = "select * from admin where name='$username'";
        return $this->mysql->fetchData($sql);
    }

    /**
     * 删除管理员
     * @param type $name
     * @param type $pwd
     * @param type $p
     * @param type $nick
     * @return type
     */
    public function delAdmin($userId){
        $sql="delete from admin  where id = " . $userId;
        return $this->mysql->executeData($sql);


    }

    /**
     * 更新状态
     * @param type $adminId
     * @param type $status
     * @return type
     */
    public function updateStatus($adminId, $status) {
        $sql = "update admin set status = " . $status . " where id = " . $adminId;
        return $this->mysql->executeData($sql);
    }

    /**
     * 重置密码
     * @param type $adminId
     * @param type $password
     * @return type
     */
    public function resetPwd($adminId, $password) {
        $sql = "update admin set pwd = '" . $password . "' where id = " . $adminId;
        return $this->mysql->executeData($sql);
    }

    /**
     * 修改密码
     * @param type $adminId
     * @param type $oldPwd
     * @param type $newPwd
     * @return type
     */
    public function modifyPwd($adminId, $oldPwd, $newPwd) {
        $sql = "update admin set pwd = '" . $newPwd . "' where id = " . $adminId . " and pwd = '" . $oldPwd . "'";
        return $this->mysql->executeData($sql);
    }
    /**
     * 修改admin
     * @param type $username
     * @param type $op
     * @return type
     */

    public function editAdmin($adminId,$username, $p){
        $sql = "update admin set name = '" . $username . "',p='".$p."' where id = " . $adminId;
        return $this->mysql->executeData($sql);


    }
}
